package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "5")int pageSize){
//        Subject subject = SecurityUtils.getSubject();
//        subject.checkPermission("用户管理");
        PageInfo<User> pageInfo = userService.findByPage(pageNum, pageSize, getLoginCompanyId());
        //存储到域对象
        request.setAttribute("pageInfo",pageInfo);
        return "system/user/user-list";
    }

    /**
     * url:/system/User/toAdd.do
     * 作用：进入添加部门页面
     * 参数：无
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        //查询所有部门
        String companyId=getLoginCompanyId();
        List<Dept> deptList = deptService.findAll(getLoginCompanyId());
        //把部门的信息存储到域钟
        request.setAttribute("deptList",deptList);
        return "system/user/user-add";
    }

    /**
     * url:/system/User/edit.do
     * 作用：新增部门||更新部门
     * 参数：部门对象
     * 返回值：部门列表页面
     */
    @RequestMapping("/edit")
    public String edit(User user){
        String companyId=getLoginCompanyId();
        String companyName=getLoginCompanyName();
        user.setCompanyId(companyId);
        user.setCompanyName(companyName);
        if (StringUtils.isEmpty(user.getId())){
            userService.save(user);
            //保存完用户，马上向mq发送消息
            Map<String, Object> map = new HashMap<>();
            map.put("email", user.getEmail());
            map.put("title", "欢迎加入传智");
            map.put("content", "谢谢合作");
            rabbitTemplate.convertAndSend("msg.email", map);
        }else {
            userService.update(user);
        }
        return "redirect:/system/user/list.do";
    }
    /**
     * /system/User/toUpdate.do?id=${User.id}
     * 作用：进入更新部门页面
     * 部门的id
     * 返回值：部门更新页面
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        //1.根据id查询当前用户信息
        User user = userService.findById(id);

        //2. 查询所有部门信息
        String companyId= getLoginCompanyId();
        List<Dept> deptList = deptService.findAll(getLoginCompanyId());
        //3. 把用户的信息存储到域中
        request.setAttribute("deptList", deptList);
        request.setAttribute("user",user);


        return "system/user/user-update";
    }

    /**
     * /system/User/delete.do?id="+id
     * 作用：删除部门
     * 返回值：map对象的json字符串
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object>delete(String id){
       Map<String,Object>resultMap= new HashMap<String,Object>();
       boolean flag=userService.delete(id);
       if (flag){
           resultMap.put("message","删除成功");
       }else {
           resultMap.put("message","删除失败，因为被分配了角色");
       }
       //删除失败
        resultMap.put("flag",flag);
       return resultMap;
    }

    /**
     * system/user/roleList.do?id="+id;
     * 进入用户角色的页面
     * 参数：id用户的id
     * 返回：用户角色页面 system/user/user-role.jsp
     */
    @RequestMapping("/roleList")
    public String roleList(String id){
        User user = userService.findById(id);
        request.setAttribute("user",user);
        //查询所有的角色
        List<Role> roleList = roleService.findAll(getLoginCompanyId());
        request.setAttribute("roleList",roleList);
        //当前用户具备的角色
       List<Role>userRoleList= roleService.findUserRoleByUserId(id);
       request.setAttribute("userRoleList",userRoleList);
       return "system/user/user-role";

    }
    /**
     * system/user/changeRole.do";
     * 作用：修改用户的角色
     * 参数：userid roleids
     * 返回值：用户列表页面
     */

    @RequestMapping("/changeRole")
    public String changeRole(String userid,String[] roleIds){
        userService.changeRole(userid,roleIds);
        return "redirect:/system/user/list.do";
    }
}
